<template>
  <button type="button" class="lineBtn" @click="upload_zip" style="width: 100%; margin-left: 0">下载文件</button>
</template>

<script setup>
import UploadZip from '@/pages/Acomponents/UploadZip/UploadZip.vue';

const fileUrl = '/static/归档.zip'; // 放在static文件夹下

const upload_zip = () => {
  try {
    uni.showToast({
      title: '开始传输',
      icon: 'success'
    });
    uni.uploadFile({
      url: 'http://172.16.9.205:3000/downloadFile', // 上传接口地址
      filePath: fileUrl, // 本地文件路径
      name: 'file', // 文件字段名，与服务器接口一致
      success: (uploadResponse) => {
        console.log('Upload success:', uploadResponse);
        if (uploadResponse.statusCode === 200) {
          console.log('Server response:', uploadResponse.data);

          uni.showToast({
            title: '传输成功',
            icon: 'success'
          });
        } else {
          console.error('Server error:', uploadResponse.data);
          uni.showToast({
            title: '传输失败',
            icon: 'error'
          });
        }
      },
      fail: (error) => {
        console.error('Upload error:', error.message);
      }
    });
  } catch (error) {
    console.error('Error:', error);
    uni.showToast({
      title: '传输出错',
      icon: 'error'
    });
  }
};
</script>

<style scoped lang="scss">
.lineBtn {
  display: block;
  width: 80%;
  margin-left: 10%;
  padding: 0;
  height: 50px;
  border-radius: 100px;
  line-height: 50px;
  background: #1e9fff;
  color: #ffffff;
  border: none;
  font-size: 16px;
}
</style>
